package tutorial.mybatis.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class UtilityMyBatis {

	private SqlSessionFactory sqlSessionFactory;
	private String myBatisConfigFileUrl;
	
	public UtilityMyBatis(String myBatisConfigFileUrl){
		this.myBatisConfigFileUrl = myBatisConfigFileUrl;
		
	}
	
	public SqlSessionFactory getSqlSessionFactory(){
		
		if(this.sqlSessionFactory == null){
			try {
				InputStream inputStream = Resources.getResourceAsStream(myBatisConfigFileUrl);
				this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return this.sqlSessionFactory;
		
	}
	
}
